package cn.com.cetccst.appshield.ui;

import static cn.com.cetccst.appstore.tools.AppAction.buttonClick;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import java.util.Objects;

import cn.com.cetccst.appshield.R;
import cn.com.cetccst.appshield.databinding.AdapterAppShieldListBinding;
import cn.com.cetccst.appstore.database.AppStoreRepository;
import cn.com.cetccst.appstore.database.model.AppModel;
import cn.com.cetccst.datacenter.policy.entity.BWAppEntity;
import cn.com.cetccst.tools.pm.AppPackage;
import cn.com.cetccst.uicomponents.binding.recyclerview.SimpleDataBindingAdapter;

public class AppInfoListAdapter extends SimpleDataBindingAdapter<BWAppEntity, AdapterAppShieldListBinding> {

    private final AppShieldFragment.OnFragmentInteractionListener listener;

    public AppInfoListAdapter(Context context, AppShieldFragment.OnFragmentInteractionListener listener) {
        super(context, R.layout.adapter_app_shield_list, new DiffUtil.ItemCallback<>() {
            @Override
            public boolean areItemsTheSame(@NonNull BWAppEntity oldItem, @NonNull BWAppEntity newItem) {
                return oldItem.getPackageName().equals(newItem.getPackageName());
            }

            @Override
            public boolean areContentsTheSame(@NonNull BWAppEntity oldItem, @NonNull BWAppEntity newItem) {
                boolean result = oldItem.getName().equals(newItem.getName())
                        && oldItem.getDescription().equals(newItem.getDescription())
                        && Objects.equals(oldItem.getDescription(), newItem.getDescription())
                        && Objects.equals(oldItem.getVersionName(), newItem.getVersionName())
                        && Objects.equals(oldItem.getAppSource(), newItem.getAppSource())
                        && Objects.equals(oldItem.getSize(), newItem.getSize())
                        && Objects.equals(oldItem.getImgUrl(), newItem.getImgUrl())
                        && Objects.equals(oldItem.isWhite(), newItem.isWhite())
                        && Objects.equals(oldItem.getAppModelId(), newItem.getAppModelId())
                        && Objects.equals(oldItem.getAppModelHash(), newItem.getAppModelHash());
                return result;
            }
        });

        this.listener = listener;

        setOnItemClickListener((item, position) -> listener.onListFragmentInteraction(item));
    }

    @Override
    protected void onBindItem(AdapterAppShieldListBinding binding, BWAppEntity item, RecyclerView.ViewHolder holder) {
        binding.setAppEntity(item);
        binding.setAction(new ActionProxy(item));
    }

    public class ActionProxy {

        private BWAppEntity appEntity;
        private AppModel appModel;

        public ActionProxy(BWAppEntity entity) {
            setEntity(entity);
        }

        public BWAppEntity getAppEntity() {
            return appEntity;
        }

        public void setAppEntity(BWAppEntity appEntity) {
            setEntity(appEntity);
        }

        private void setEntity(BWAppEntity appEntity) {
            this.appEntity = appEntity;
            if (!TextUtils.isEmpty(appEntity.getAppModelId())) {
                appModel = AppStoreRepository.getInstance().getAppByAppId(appEntity.getAppModelId());
            }
        }

        public void buttonAction(View view) {
            //这里的业务对应 buttonText 的展示
            if (appEntity.isWhite()) {
                if (appModel != null) {
                    buttonClick(context, appModel);
                    return;
                }
                AppPackage.launchAppByPackageName(context, appEntity.getPackageName());
            } else {
                AppPackage.uninstallApk(context, appEntity.getPackageName());
            }
        }

        public boolean buttonEnabled() {
            if (!appEntity.isWhite()) {
                if (appEntity.getAppSource() == BWAppEntity.SOURCE_SYSTEM)
                    return false;
                if (!AppPackage.isPackageFound(context, appEntity.getPackageName()))
                    return false;
            }
            return appEntity.getAppSource() != BWAppEntity.SOURCE_NONE;
        }

        public int buttonText() {
            if (appEntity.isWhite()) {
                if (appModel != null)
                    return appModel.getResIdByStatus();
                return cn.com.cetccst.appstore.R.string.store_open;
            } else {
                return cn.com.cetccst.appstore.R.string.store_uninstall;
            }
        }

        public Drawable getIcon() {
            return AppPackage.getDrawable(context, appEntity.getPackageName());
        }

        public AppModel getAppModel() {
            return appModel;
        }
    }
}
